package cn.gzmli.dtt.common.entity;

import cn.gzmli.dtt.common.entity.vo.ProjectVO;
import cn.gzmli.dtt.common.entity.vo.UserVO;
import cn.gzmli.dtt.sbzyc.validation.Add;
import cn.gzmli.dtt.sbzyc.validation.Delete;
import cn.gzmli.dtt.sbzyc.validation.Update;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Descript
 * @Author xsx
 * @Date 2017/12/25
 */
@Entity
@Table(name = "user")
@Data
public class User implements Serializable,Cloneable {

    private static final long serialVersionUID = -4444411833171987614L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull(groups = {Update.class, Delete.class})
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "departmentId", referencedColumnName = "id")
    private Department department;

    @NotBlank(groups = Add.class)
    private String userName;

    @NotBlank(groups = Add.class)
    private String password;

    @NotBlank(groups = Add.class)
    private String name;

    private Integer sex;
    private String phone;
    private String email;
    private String description;
    private Integer status = 1;
    private String headerpath;
    private String userNo;
    private String clientId;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "user_role",
            joinColumns = {@JoinColumn(name = "userId", referencedColumnName = "id", table = "user")},
            inverseJoinColumns = {@JoinColumn(name = "roleId", referencedColumnName = "id", table = "role")}
    )
    private Set<Role> roles;


//    @ManyToMany(fetch = FetchType.LAZY)
//    @JoinTable(
//            name = "user_mac",
//            joinColumns = {@JoinColumn(name = "userId", referencedColumnName = "id", table = "user")}
//    )
//    private Set<UserMac> UserMacs;


    //用户机构IDS
    @Transient
    private String userOrgIDS;


    //用户mac地址
    @Transient
    private Set<String> userMacs;

    //用户机构信息
    @Transient
    private List<UserVO> userOrg;

    //用户机构角色IDS
    @Transient
    private String userOrgRoleIDS;

    //用户机构角色信息
    @Transient
    private List<UserVO> userOrgRole;

    //用户项目IDS
    @Transient
    private String userProjectIDS;

    //用户当前选择的项目ID
    @Transient
    private Integer userCurrentProjectId;

    //用户项目标段IDS
    @Transient
    private String userProjectSectionIDS;

    //用户项目信息
    @Transient
    private List<ProjectVO> userProject;

    //用户项目标段信息
    @Transient
    private List<ProjectVO> userProjectSection;

    //用户角色IDS
    @Transient
    private String userRoleIDS;
    //用户角色名称
    @Transient
    private String userRoleNames;

    //用户角色菜单IDS
    @Transient
    private String userRoleMenuIDS;
    //用户角色菜单名称
    @Transient
    private String userRoleMenuNames;

    //用户区域IDS
    @Transient
    private String regionIds;

    //用户路线IDS
    @Transient
    private String routeIds;

    //用户路段IDS
    @Transient
    private String sectionIds;

    //用户场地IDS
    @Transient
    private String siteIds;

    //用户Map对象
    @Transient
    private Map<String,Object> dataMap;

    //用户Object对象
    @Transient
    private List<Object> dataObject;

    public String getUserProjectIDS() {
        if(StringUtils.isEmpty(userProjectIDS)){
            return "";
        }else {
            return userProjectIDS;
        }
    }

    public void setUserCurrentProjectIdbyIDS(String userCurrentProjectIds) {
        String[] ids = userCurrentProjectIds.split(",");
        if(ids!=null&&ids.length>0){
            this.userCurrentProjectId = Integer.valueOf(ids[0]);
        }
    }

    public void setUserProjectSectionIDS(String userProjectSectionIDS) {
        if(userProjectSectionIDS!=null&&!userProjectSectionIDS.equals("")){
            String[] sectionIdsArray=null;
            String sectionIdsStr="";
          if(userProjectSectionIDS.indexOf(",")!=-1){
              sectionIdsArray=userProjectSectionIDS.split(",");
          }else{
              sectionIdsArray=new String[]{userProjectSectionIDS};
          }
            for (int i = 0; i < sectionIdsArray.length; i++) {
                if (!sectionIdsArray[i].trim().toUpperCase().equals("NULL") &&
                        !sectionIdsArray[i].trim().toUpperCase().equals("")) {
                    if (i + 1 < sectionIdsArray.length) {
                        sectionIdsStr += sectionIdsArray[i] + ",";
                    } else {
                        sectionIdsStr += sectionIdsArray[i];
                    }
                }
            }
            this.userProjectSectionIDS=sectionIdsStr;
        }else{
            this.userProjectSectionIDS=null;
        }
    }

    public String getRoleIDS() {
        StringBuilder ids = new StringBuilder();
        if (this.roles != null) {
            for (Role role : this.roles) {
                    ids.append(role.getId()).append(",");
            }
        }
        return ids.substring(0, ids.length() > 1 ? ids.length() - 1 : 0);
    }

    public void loadRoleAndMenu() {
        if (this.roles != null) {
            for (Role role : this.roles) {
                role.getMenuIDS();
            }
        }
    }

    public Object clone() throws CloneNotSupportedException{
        return super.clone();
    }

}
